/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.validator;

import java.io.*;
import java.util.*;
import ch.simpleel.util.*;

/**
 * Result of an Bean validation.
 * 
 * @author M. Hautle
 */
public class ValidationResult implements Serializable {
    /** Psuedo expression for general errors. */
    public static String GENERAL = "#";

    /** Map holding an el expression describing the property and the error objects for this property */
    private Map<String, IErrorDescription[]> errors = new HashMap<String, IErrorDescription[]>();

    /**
     * Returns the errors of an single property
     * 
     * @param el The el expression describing the property (normaly simply the property name)
     * @return An list of errors or null if the property was valid
     */
    public IErrorDescription[] getErrors(String el) {
        return errors.get(el);
    }

    /**
     * Returns wherever the validated bean was valid.
     * 
     * @return True if the validated bean was valid
     */
    public boolean isBeanValid() {
        return errors.size() == 0;
    }

    /**
     * Returns the error map.
     * 
     * @return The error map
     */
    public Map<String, IErrorDescription[]> getAllErrors() {
        return Collections.unmodifiableMap(errors);
    }

    /**
     * Adds the given error for the given property.
     * 
     * @param el The el expression describing the property
     * @param error The errors to add
     */
    public void addErrors(String el, IErrorDescription... error) {
        final IErrorDescription[] msg = errors.get(el);
        // we expect that the main scenario is to add only once errors per property...
        errors.put(el, msg == null ? error : merge(msg, error));
    }

    /**
     * Merges the two arrays into one.<br>
     * Used instead of {@link MiscUtils#arrayAppend(Object[], Object...)} to avoid additional dependencies.
     * 
     * @param e1 The first array
     * @param e2 The second array
     * @return The merged array
     */
    private IErrorDescription[] merge(IErrorDescription[] e1, IErrorDescription[] e2) {
        final IErrorDescription[] merged = new IErrorDescription[e1.length + e2.length];
        System.arraycopy(e1, 0, merged, 0, e1.length);
        System.arraycopy(e2, 0, merged, e1.length, e2.length);
        return merged;
    }
}
